import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.nio.file.*;
import java.util.*;

public class ReportSubmissionChecker {

    public static void main(String[] args) throws IOException {
        // 指定文件夹路径和Excel文件路径
        String folderPath = "path/to/your/folder";
        String excelFilePath = "path/to/your/excel.xlsx";

        // 读取Excel文件中的名单
        Set<String> studentList = readStudentList(excelFilePath);

        // 获取文件夹中所有docx文件的学号姓名
        Set<String> submittedFiles = getSubmittedFiles(folderPath);

        // 比较名单，找出未提交的学生
        List<String> missingSubmissions = findMissingSubmissions(studentList, submittedFiles);

        // 输出未提交的学生名单
        System.out.println("未提交报告的学生名单:");
        missingSubmissions.forEach(System.out::println);
    }

    private static Set<String> readStudentList(String excelFilePath) throws IOException {
        Set<String> studentList = new HashSet<>();
        try (InputStream inputStream = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(inputStream);
             Sheet sheet = workbook.getSheetAt(0)) {

            for (Row row : sheet) {
                // 假设学号姓名在第一列
                Cell cell = row.getCell(0);
                String studentInfo = cell.getStringCellValue();
                studentList.add(studentInfo);
            }
        }
        return studentList;
    }

    private static Set<String> getSubmittedFiles(String folderPath) throws IOException {
        Set<String> submittedFiles = new HashSet<>();
        Path path = Paths.get(folderPath);
        DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.docx");

        for (Path entry : stream) {
            String fileName = entry.getFileName().toString();
            // 假设文件名格式为"学号姓名.docx"
            String studentInfo = fileName.substring(0, fileName.length() - 5);
            submittedFiles.add(studentInfo);
        }
        return submittedFiles;
    }

    private static List<String> findMissingSubmissions(Set<String> studentList, Set<String> submittedFiles) {
        List<String> missingSubmissions = new ArrayList<>();
        studentList.removeAll(submittedFiles); // 从名单中移除已提交的学生
        missingSubmissions.addAll(studentList); // 剩余的就是未提交的学生
        return missingSubmissions;
    }
}